
//获取初始化数据
var q = {
    pagenum: 1,
    pagesize: 3,
    cate_id: '',
    state: '',

}

initArtList(q)
getCate()

//利用事件委托为筛选区域表单绑定表单提交事件
$('.layui-card-body').on('submit', '.layui-form', function (e) {
    e.preventDefault()
    q.cate_id = $('[name=cate_id]').val()
    q.state = $('[name=state]').val()
    initArtList(q)
})


//为当前文章删除绑定点击事件
$('.layui-card-body').on('click', '.remove', function () {
    var thisId = $(this).attr('data-id')
    layer.confirm('确定删除？', { icon: 3, title: '删除文章' }, function (index) {
        //do something
        var len = $('.remove').length
        deleteArt(thisId, len)
        layer.close(index);
    });
})


//为当前文章编辑绑定点击事件
$('.layui-card-body').on('click', '.modify', function () {
    let bookId = $(this).attr('data-id')
    location.href = '/article/art_pub.html?Id=' + bookId
})



//初始化文章列表数据
function initArtList(q) {
    $.ajax({
        type: 'GET',
        url: '/my/article/list',
        data: q,
        success: function (res) {
            if (res.status !== 0) return layui.layer.msg(res.message)
            // layui.layer.msg(res.message)
            //将数据动态渲染至页面
            var htmlStr = template('artlist', res)
            $('#art-list tbody').html(htmlStr)

            //文章列表分页
            layui.use('laypage', function () {
                const laypage = layui.laypage
                laypage.render({
                    elem: 'page',
                    count: res.total,
                    limit: q.pagesize,
                    curr: q.pagenum,
                    limits: [3, 5, 8, 10],
                    layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
                    jump: function (obj, first) {
                        q.pagenum = obj.curr
                        q.pagesize = obj.limit
                        if (!first) {
                            initArtList(q)
                        }
                    }
                })
            })
        }
    })
}

//封装函数发送ajax根据id删除文章
function deleteArt(id, len) {
    $.ajax({
        type: 'GET',
        url: '/my/article/delete/' + id,
        success: function (res) {
            if (res.status !== 0) return layui.layer.msg(res.message)
            layui.layer.msg(res.message)
            if (len == 1) {
                q.pagenum--
                if (q.pagenum == 1) {
                    q.pagenum = 1
                }
            }
            initArtList(q)
        }
    })
}



//定义template的过滤器（函数）美化时间
template.defaults.imports.dataTime = function (value) {
    let dt = new Date(value)
    let yy = dt.getFullYear()
    let mon = dt.getMonth() + 1
    let dd = dt.getDate()
    let hh = dt.getHours()
    let min = dt.getMinutes()
    let ss = dt.getSeconds()
    return yy + '-' + zero(mon) + '-' + zero(dd) + '   ' + zero(hh) + ':' + zero(min) + ':' + zero(ss)
}
//为日期补零
function zero(n) {
    return n = n < 10 ? '0' + n : n
}

//定义封装获取文章分类初始化
function getCate() {
    $.ajax({
        type: 'GET',
        url: '/my/article/cates',
        success: function (res) {
            if (res.status !== 0) return layui.layer.msg(res.message)
            var htmlStr = template('cate_name', res)
            $('#J_cate').html(htmlStr)
            layui.form.render()
        }
    })
}
